import Vue from 'vue'

import utils from './utils'
import config from './config'

const http = {

	/**
	 * 定义请求头
	 */
	header: {
		'x-requested-with': 'XMLHttpRequest'
	},

	/**
	 * get 请求
	 * @param {Object} url 请求地址
	 * @param {Object} data 请求数据
	 * @param {Object} success 请求成功回调
	 * @param {Object} stip 请求成功提示
	 * @param {Object} etip 请求失败提示
	 * @param {Object} loading 请求是否显示加截中
	 */
	get: function(url, data, success, stip, etip, loading) {
		if (url.indexOf('/oauth/token') > -1) {
			this.header['Authorization'] =
				'basic c3RyaXZlX2FwcF9pZF9zeXNwbGF0Zm9ybTpzdHJpdmVfYXBwX3NlY3JldF9zeXNwbGF0Zm9ybQ==';
		} else if (url.indexOf('/admin') > -1) {
			this.header['Authorization'] = 'bearer ' + uni.getStorageSync(config.tokenName);
		} else {
			this.header['Authorization'] = '';
		}
		loading && utils.showLoading()
		uni.request({
			url: config.baseUrl + url,
			data: data,
			header: this.header,
			method: 'GET',
			dataType: 'json',
			responseType: 'text',
			success: function(res) {
				let resData = (res && res.data) || {};
				if (resData.status == "success" || resData.access_token) {
					stip && utils.showToast("操作成功！");
					success && success(resData);
				} else if (resData.status == "notAuthentication") {
					utils.showModal("请先登录！", () => {
						uni.redirectTo({
							url: '/pages/login/index'
						});
					}, true);
				} else {
					etip && utils.showModal(resData.message || '请求错误');
				}
			},
			fail: function(res) {
				etip && utils.showToast();
			},
			complete: function(res) {
				loading && utils.hideLoading();
				uni.stopPullDownRefresh();
			},
		})
	},

	/**
	 * post 请求
	 * @param {Object} url 请求地址
	 * @param {Object} data 请求数据
	 * @param {Object} success 请求成功回调
	 * @param {Object} stip 请求成功提示
	 * @param {Object} etip 请求失败提示
	 * @param {Object} loading 请求是否显示加载中
	 */
	post: function(url, data, success, stip, etip, loading) {
		if (url.indexOf('/oauth/token') > -1) {
			this.header['Authorization'] =
				'basic c3RyaXZlX2FwcF9pZF9zeXNwbGF0Zm9ybTpzdHJpdmVfYXBwX3NlY3JldF9zeXNwbGF0Zm9ybQ==';
		} else if (url.indexOf('/admin') > -1) {
			this.header['Authorization'] = 'bearer ' + uni.getStorageSync(config.tokenName);
		} else {
			this.header['Authorization'] = '';
		}
		loading && utils.showLoading()
		uni.request({
			url: config.baseUrl + url,
			data: data,
			header: this.header,
			method: 'POST',
			dataType: 'json',
			responseType: 'text',
			success: function(res) {
				let resData = (res && res.data) || {};
				if (resData.status == "success" || resData.access_token) {
					stip && utils.showToast("操作成功！");
					success && success(resData);
				} else if (resData.status == "notAuthentication") {
					utils.showModal("请先登录！", () => {
						uni.redirectTo({
							url: '/pages/login/index'
						});
					}, true);
				} else {
					etip && utils.showModal(resData.message || '请求错误');
				}
			},
			fail: function(res) {
				etip && utils.showToast();
			},
			complete: function(res) {
				loading && utils.hideLoading();
				uni.stopPullDownRefresh();
			},
		})
	},

	upload: function() {},

	download: function() {},
}

Vue.prototype.$request = http

export default http
